package invertedindex;

import java.sql.*;
import java.util.*;

public class ConnectionManager {
    private static Stack<Connection> connections = new Stack<Connection>();
    static {
    	try { 
    		DriverManager.registerDriver (new oracle.jdbc.driver.OracleDriver());
    	}
    	catch (SQLException e) {
    		System.err.println("Error registering JDBC driver: " + e);
    	}; 		
    }
    
    public static Connection getConnection() throws Exception {
        if (!connections.empty())
            return connections.pop();
        else {//No one left in the stack, create a new one
        	Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:TEACH", "alimjiwa", "stv00765662");
        	System.out.println("New database connection established");
        	return conn;
        }
        
    }
    
    public static void returnConnection(Connection conn) {
        if (conn != null) connections.push(conn);
    }      
}